<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\User;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\User\Ctl\LogoutCtl;
use Srv\Apps\Proto\DTO\User\LogoutListDTO;
use Srv\Apps\Proto\DTO\User\LogoutReviewDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\User\LogoutListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class LogoutAct extends ActionAbstract
{
    /**
     * @url /logout/list
     * @auth getLogoutList
     * @req Srv.Apps.Proto.DTO.User.LogoutListDTO
     * @rsp Srv.Apps.Proto.VO.User.LogoutListVO
     * 注销记录列表
     */
    public function getLogoutList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof LogoutListDTO) || !($VOClass instanceof LogoutListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = LogoutCtl::tag()->getLogoutList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass);
    }


    /**
     * @url /logout/review/set
     * @auth setLogoutReview
     * @req Srv.Apps.Proto.DTO.User.LogoutReviewDTO
     * 注销审核
     */
    public function setLogoutReview(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof LogoutReviewDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData                = $RouteConfData->getParamsResData($DTOClass);
        $logData                = $setData;
        $LogCtl                 = LogCtl::tag();
        $LogoutCtl              = LogoutCtl::tag();
        $ultrIdList             = $setData[LogoutReviewDTO::_ultrIdList]??[];
        $review                 = $setData[LogoutReviewDTO::_review]??-1;
        $reviewRemark           = $setData[LogoutReviewDTO::_reviewRemark]??'';
        if(!$LogoutCtl->checkReviewStatus($ultrIdList)){
            $errMsg             = '参数有误';
            $LogCtl->logAdd(LogCtl::LOGOUT_REVIEW_SET, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_ERROR, Lang::getString(LangKeyEnum::_LANG_ERROR));
        }
        $result                 = $LogoutCtl->setLogoutReview($ultrIdList, $review, $reviewRemark);
        if(!$result){
            $errMsg             = '注销审核失败';
            $LogCtl->logAdd(LogCtl::LOGOUT_REVIEW_SET, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg                 = '注销审核成功';
        $LogCtl->logAdd(LogCtl::LOGOUT_REVIEW_SET, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}